const Koa = require('koa');
var Router = require('koa-router');
// const bodyparser = require("koa-bodyparser")
const bodyparser = require("koa-body")
const { access } = require("../middle/index")
let app = new Koa()
var router = new Router();
app.use(access)
app.use(bodyparser())
app.use(async (ctx, next) => {
  const { getConnectionPool } = require("./db")
  // console.log("连接池开启")
  ctx.app.mssqlPool = await getConnectionPool().connect()
  await next()
  ctx.app.mssqlPool.close()
  // console.log("连接池关闭")
})
app = require("./route/index")(app, router)
app.listen(4000,()=>{
  console.log(`服务在http://127.0.0.1:4000端口启动`)
})
/**
 * 开始和结束开启数据池，关闭数据池
 */
// router.get('/list', async (ctx, next) => {
//   // ctx.router available
//   try {
//     const pool = await getConnectionPool().connect()
//     const body = await pool.query(`SELECT TOP 1000 * FROM [dbo].[GIS_T_TZFM_V]`)
//     pool.close()
//     // ctx.status = 200;
//     // ctx.type = 'application/json';
//     ctx.body = body
//   } catch (error) {
//     console.log(error)
//     ctx.body = '请检查url';
//     ctx.status = 404;
//   }
// });